package com.shawn.integeration.business.user.service;

import cn.hutool.json.JSONUtil;
import com.shawn.springboot.integeration.dubbo.api.common.Response;
import com.shawn.springboot.integeration.dubbo.api.common.ResponseCode;
import com.shawn.springboot.integeration.dubbo.api.user.IUserService;
import com.shawn.springboot.integeration.dubbo.api.user.dto.UserReqDTO;
import com.shawn.springboot.integeration.dubbo.api.user.dto.UserResDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;

/**
 * @author shawn
 * @description: 用户信息接口实现
 * @date 2024/1/6
 */
@Slf4j
@DubboService(version = "1.0.0")
public class UserServiceImpl implements IUserService {
    @Override
    public Response<UserResDTO> queryUserInfo(UserReqDTO req) {
        log.info("查询用户信息:[{}]", JSONUtil.toJsonStr(req));
        try {
            // 1. 模拟从数据库中查询
            UserResDTO resDTO = UserResDTO.builder()
                    .userId(req.getUserId())
                    .userName("shawn")
                    .userAge(20)
                    .build();

            // 2. 返回结果
            return Response.<UserResDTO>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .data(resDTO).build();
        } catch (Exception e) {
            log.error("查询用户信息失败,userId:{}", req.getUserId(), e);
            return Response.<UserResDTO>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(ResponseCode.UN_ERROR.getInfo())
                    .build();
        }
    }
}
